package com.example.demo.web;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.serve.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("user")
public class IndexController {
    @Autowired
    private Userservice userservice;

//    @ResponseBody
//    @RequestMapping
//    public List<User> index() {
//        List<User> users = userservice.findUsers();
//        return users;
//    }

    @RequestMapping("list")
    public String index2(ModelMap map) {
        List<User> users = userservice.findUsers();
        map.put("users", users);
        return "user/userlist";
    }


    @PostMapping("update")
    public String update(User user) {
        userservice.update(user);
        return "redirect:/user/list";
    }

    @GetMapping("edit/{age}")
    public String edit(@PathVariable("age") String age, ModelMap map) {
        User user = userservice.get(age);
        map.put("user", user);
        return "user/edit";
    }

    @GetMapping("add")
    public String add() {
        return "user/add";
    }

    @RequestMapping("save")
    public String save(User user) {
        userservice.save(user);
        return "redirect:/user/list";
    }

    @GetMapping("delete/{age}")
    public String dalete(@PathVariable("age") String age) {
        userservice.delete(age);
        return "redirect:/user/list";
    }

}
